package Presenter;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import Model.Game;
import Model.Player;


/**
 * Runs each round
 * @author Phillip
 *
 */
public class Round {
	
	
	private static Player[] playerOrder;
	private Game g;
	private MainGameScreen gameScreen;
	private JLabel nextTurnMessage; 
	private Turn turn;
	
	/**
	 * Getter for the round number
	 * @return roundnum
	 */


	/**
	 * Increments the round count
	 */

	
	
	
	/**
	 * returns the food required for full time allotment 
	 */
	public int getFoodNeeded() {
		
		if (g.getCurrentRound() <= 4) { return 3; }
		
		else if (g.getCurrentRound() <= 8) { return 4; }
		
		else { return 5; }
	}
		

	/**
	 * Constructs the Round object
	 * @param g
	 * @param gameScreen
	 */
	public Round(Game g, MainGameScreen gameScreen) {
		this.g = g;
		playerOrder = g.getPlayers();
		this.gameScreen = gameScreen;
		turn = new Turn(g, gameScreen);
		
	}
	
	/**
	 * Sets the order of the players based on their scores
	 * @param players
	 */
	public void orderPlayers(){
		ArrayList<Player> playersAl = new ArrayList<Player>();
		for(Player player: g.getPlayers()){
			System.out.println();
			playersAl.add(player);
		}
		
		Collections.sort(playersAl);
			
		Player[] newPlayers = new Player[4];	
		for(int i = 0; i<playersAl.size(); i++) {
			newPlayers[i] = playersAl.get(i);
		}
		g.setPlayers(newPlayers);
		playerOrder = newPlayers;
		System.out.println("Player order for next round:");
		for(int x = 0; x<newPlayers.length; x++) {
			System.out.println(newPlayers[x].getName());
		}
	}
	
	/**
	 * Increments the round count
	 */
	public void nextRound() {
		g.incrementRound();
	}
	
	/**
	 * Starts each round, shows JOptionPanes with player instructions
	 */
	public void startRound() {
		if(g.getCurrentRound() == g.getTotalRounds()) {
			JOptionPane.showMessageDialog(null, "The Game is Over");
			System.exit(0);
		} else if(g.getCurrentRound() ==1) {
			firstRound();
		}	else {
			
			orderPlayers();
			
			turn.setCurrentPlayer(0);
			
			turn.startTurn();

		}
		
	}
	
	/**
	 * Sets conditions for the first round
	 */
	public void firstRound() {
//		nextTurnMessage = new JLabel();
//		nextTurnMessage.setText(g.getPlayers()[0].getName() + ", please select your free land");
//		gameScreen.add(nextTurnMessage,0);
//		nextTurnMessage.setOpaque(false);
//		nextTurnMessage.setVisible(true);
//		nextTurnMessage.setLocation(750, 100);
//		nextTurnMessage.setFont(new Font("Helvetica", Font.PLAIN, 30));
//		nextTurnMessage.setForeground(new Color(204, 153, 0));	
		turn.setCurrentPlayer(0);
	}
	
	/**
	 * Peforms cleanup actions at the end of each round
	 */
	public void endRound() {
		EndRoundScreen endRound = new EndRoundScreen(g, gameScreen);
		gameScreen.add(endRound, "RoundEnd");
		
		g.getMap().produce(); // do production
		g.incrementRound();
		gameScreen.refreshTownPresenter(); // make sure townPresenter() has updated player quantities
		gameScreen.getRound().startRound();

		
		gameScreen.getCounter().set(1);
		gameScreen.getLayoutManager().addLayoutComponent(endRound, "RoundEnd");
		gameScreen.getLayoutManager().show(gameScreen, "RoundEnd");
		
		
		
		g.incrementRound();
	}
	
	/**
	 * Getter for turn
	 * @return turn
	 */
	public Turn getTurn() {
		return turn;
	}
	
	
}
